<?
use adminmatrix\migration\Migrator;
use  adminmatrix\migration\db\Column;

class Organization extends Migrator
{
    /**
         * Change Method.
         *
         * Write your reversible migrations using this method.
         *
         * More information on writing migrations is available here:
         * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
         *
         * The following commands can be used in this method and Phinx will
         * automatically reverse them when rolling back:
         *
         *    createTable
         *    renameTable
         *    addColumn
         *    renameColumn
         *    addIndex
         *    addForeignKey
         *
         * Remember to call "create()" or "update()" and NOT "save()" when working
         * with the Table class.
         */
    public function up()
    {
        $this->createOrganizationTable();
        $this->createDepartmentTable();
        $this->createPositionTable();
        $this->createUserOrganizationTable();
    }

    public function down()
    {
        $this->table('admin_organization')->drop()->save();
        $this->table('admin_department')->drop()->save();
        $this->table('admin_position')->drop()->save();
        $this->table('admin_user_organization')->drop()->save();
    }

    private function createOrganizationTable()
    {
        $table = $this->table('admin_organization', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '组织表',
            'id' => 'id',
            'primary_key' => ['id']
        ]);

        $table
            ->addColumn('name', 'string', [
                'null' => false,
                'limit' => 100,
                'comment' => '组织名称'
            ])
            ->addColumn('description', 'string', [
                'null' => true,
                'limit' => 255,
                'comment' => '组织描述'
            ]);

        $table->addIndex('name', [
            'name' => 'name',
            'unique' => true
        ]);

        $table->create();
    }

    private function createDepartmentTable()
    {
        $table = $this->table('admin_department', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '部门表',
            'id' => 'id',
            'primary_key' => ['id']
        ]);

        $table
            ->addColumn('name', 'string', [
                'null' => false,
                'limit' => 100,
                'comment' => '部门名称'
            ])
            ->addColumn('organization_id', 'integer', [
                'null' => false,
                'limit' => 11,
                'comment' => '所属组织 ID'
            ])
            ->addColumn('description', 'string', [
                'null' => true,
                'limit' => 255,
                'comment' => '部门描述'
            ]);

        $table->addIndex('name', [
            'name' => 'name',
            'unique' => false
        ]);

        $table->create();
    }

    private function createPositionTable()
    {
        $table = $this->table('admin_position', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '职位表',
            'id' => 'id',
            'primary_key' => ['id']
        ]);

        $table
            ->addColumn('name', 'string', [
                'null' => false,
                'limit' => 100,
                'comment' => '职位名称'
            ])
            ->addColumn('department_id', 'integer', [
                'null' => false,
                'limit' => 11,
                'comment' => '所属部门 ID'
            ])
            ->addColumn('description', 'string', [
                'null' => true,
                'limit' => 255,
                'comment' => '职位描述'
            ]);

        $table->addIndex('name', [
            'name' => 'name',
            'unique' => false
        ]);

        $table->create();
    }

    private function createUserOrganizationTable()
    {
        $table = $this->table('admin_user_organization', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '用户组织关联表',
            'id' => false
        ]);

        $table
            ->addColumn('user_id', 'integer', [
                'null' => false,
                'limit' => 11,
                'comment' => '用户 ID'
            ])
            ->addColumn('position_id', 'integer', [
                'null' => false,
                'limit' => 11,
                'comment' => '职位 ID'
            ]);

        $table->addIndex(['user_id', 'position_id'], [
            'name' => 'user_position_index',
            'unique' => false
        ]);

        $table->create();
    }
}